package com.example.kotlintest.chat.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.kotlintest.R;
import com.example.kotlintest.chat.EmojiUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author linzi
 * @date 2022-9-28
 * 表情适配器
 */
public class MaskAdapter extends RecyclerView.Adapter<MaskAdapter.MaskHodler> {

    private Context context;
    private OnItemClickListener onItemClickListener;
    private int[] res=EmojiUtils.getEmojis();

    public static void init(RecyclerView recyclerView,Context context,OnItemClickListener onItemClickListener){
        ViewGroup.LayoutParams params=recyclerView.getLayoutParams();
        params.height=500;

        GridLayoutManager gridLayoutManager=new GridLayoutManager(context,8);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setLayoutParams(params);
        MaskAdapter adapter=new MaskAdapter(context, onItemClickListener);
        recyclerView.setAdapter(adapter);
    }

    private MaskAdapter(Context context, OnItemClickListener onItemClickListener) {
        this.context = context;
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public MaskHodler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.chat_mask_item,parent,false);
        return new MaskHodler(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MaskHodler holder, @SuppressLint("RecyclerView") int position) {
        holder.ivMask.setImageResource(res[position]);
        holder.ivMask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.itemClick(holder,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return res.length;
    }

    public static class MaskHodler extends RecyclerView.ViewHolder{
        public ImageView ivMask;
        public MaskHodler(@NonNull View itemView) {
            super(itemView);
            ivMask=itemView.findViewById(R.id.ivMask);
        }
    }

    public interface OnItemClickListener{
        void itemClick(MaskAdapter.MaskHodler menuHolder, int postion);
    }
}
